using System;
using System.Linq;
using Ease.EaseMission.Editor;
using Ease.EaseMission.Editor.Node;
using Ease.EaseMission.Editor.View;
using Ease.EaseMission.Runtime;
using Ease.EaseNode.Editor.NodeView;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class MissionInitParam : InitParam
{
    public Type nodeType;
}

public class MissionEW : BaseEaseEditorWindow<MsChartView, MsInspectorView>
{
    protected override string VisualTree => "Assets/Ease/EaseMission/Editor/MissionEW.uxml";
    protected override string StyleSheet => "Assets/Ease/EaseMission/Editor/MissionEW.uss";

    private RadioButtonGroup radioButtonGroup = null;

    private MissionDataSo missionDataSo = null;

    private MissionStage missionStage = null;

    private MissionStep missionStep = null;

    protected override void OnPrepareData()
    {
        dicData2Node.Add(typeof(MissionStage), typeof(MissionStageNode));
        FindDataSo();
        DataManager = (missionDataSo)?.NodeDataManager;
    }

    private void FindDataSo()
    {
        missionDataSo = (Selection.activeObject as MissionDataSo);
        if (missionDataSo == null && Selection.activeGameObject != null)
        {
            missionDataSo = Selection.activeGameObject.GetComponent<IDataSoHolder<MissionDataSo>>()?.DataSo;
        }
        if (missionDataSo == null && MissionManager.Instanc != null)
        {
            missionDataSo = MissionManager.Instanc.DataSo;
        }
    }

    protected override void OnCreateGUIOther()
    {
        (EaseChartView as MsChartView)?.SetMenu<CreateSearchMenuWindowProvider0>();
        var inspectorView = UpdateSelection as MsInspectorView;
        inspectorView?.OnEnable();
        if (EaseChartView is MsChartView msChartView)
            msChartView.OnNodeEditor += OnNodeEditor;
        radioButtonGroup = rootVisualElement.Q<RadioButtonGroup>();
        radioButtonGroup.label = "";
        radioButtonGroup.choices = new[] { "大步骤", "小步骤", "行为" };
        radioButtonGroup.Children().FirstOrDefault()!.style.flexDirection = FlexDirection.Row;
        radioButtonGroup.RegisterValueChangedCallback(OnRadioGroupValueChange);
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        if(missionDataSo!=null)
            EditorUtility.SetDirty(missionDataSo);
        if (EaseChartView is MsChartView msChartView)
            msChartView.OnNodeEditor -= OnNodeEditor;
        var inspectorView = UpdateSelection as MsInspectorView;
        inspectorView?.OnDisable();
        radioButtonGroup.UnregisterValueChangedCallback(OnRadioGroupValueChange);
    }

    protected override void Update()
    {
        base.Update();
        if (missionDataSo == null)
        {
            FindDataSo();
            if (missionDataSo != null)
                OnChangeLayer(0);
        }
    }

    public override void OnUpdateSelection(BaseEaseNode easeNode)
    {
        base.OnUpdateSelection(easeNode);
        if (easeNode is MissionStageNode missionStageNode)
        {
            missionStage = missionStageNode.State.Data as MissionStage;
            if (missionStage != null)
                missionStep = missionStage.Steps.FirstOrDefault();
        }
        else if (easeNode is MissionStepNode missionStepNode)
        {
            missionStep = missionStepNode.State.Data as MissionStep;
        }
    }

    //点击了Node上面的编辑；
    private void OnNodeEditor(BaseEaseNode node)
    {
        if (node is MissionStageNode missionStageNode)
        {
            missionStage = missionStageNode.State.Data as MissionStage;
            OnUpdateSelection(node);
            radioButtonGroup.SetValueWithoutNotify(1);
            OnChangeLayer(1);
        }
        else if (node is MissionStepNode missionStepNode)
        {
            missionStep = missionStepNode.State.Data as MissionStep;
            OnUpdateSelection(node);
            radioButtonGroup.SetValueWithoutNotify(2);
            OnChangeLayer(2);
        }
    }

    private void OnRadioGroupValueChange(ChangeEvent<int> a)
    {
        Debug.Log($"{a.newValue}");
        OnChangeLayer(a.newValue);
    }

    private void OnChangeLayer(int layer)
    {
        if (layer == 0)
        {
            dicData2Node.Clear();
            dicData2Node.Add(typeof(MissionStage), typeof(MissionStageNode));
            if (missionDataSo != null)
                DataManager = missionDataSo.NodeDataManager;
            (EaseChartView as MsChartView)?.SetMenu<CreateSearchMenuWindowProvider0>();
            EaseChartView.Reset(new MissionInitParam() { nodeType = typeof(MissionStage), selectID = missionStage?.Id ?? -1 });
        }
        else if (layer == 1)
        {
            dicData2Node.Clear();
            dicData2Node.Add(typeof(MissionStep), typeof(MissionStepNode));
            DataManager = missionStage?.NodeDataManager;
            (EaseChartView as MsChartView)?.SetMenu<CreateSearchMenuWindowProvider1>();
            EaseChartView.Reset(new MissionInitParam() { nodeType = typeof(MissionStep), selectID = missionStep?.Id ?? -1 });
        }
        else if (layer == 2)
        {
            dicData2Node.Clear();
            dicData2Node.Add(typeof(MissionData), typeof(MissionDataNode));
            DataManager = missionStep?.NodeDataManager;
            (EaseChartView as MsChartView)?.SetMenu<CreateSearchMenuWindowProvider2>();
            EaseChartView.Reset(new InitParam());
        }
    }
}